package com.example.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {
    /**
     * Jedis
     */
    @Bean
    public JedisConnectionFactory redisConnectionFactory() {
        // 给redis设置一下主机 端口 ip等  Standalone 单机的
        RedisStandaloneConfiguration config =
                new RedisStandaloneConfiguration("175.178.217.212", 6379);
        config.setPassword("123456");
        return new JedisConnectionFactory(config);
    }

    @Bean
    public RedisTemplate redisTemplate(JedisConnectionFactory redisConnectionFactory){
        RedisTemplate template = new RedisTemplate();
        // 给redis模板 注入上面的那个工厂
        template.setConnectionFactory(redisConnectionFactory);
        // redis 默认使用了序列化 官方也推荐我们更改其他的序列化 我这里设置为String
        template.setDefaultSerializer(new StringRedisSerializer());
        return template;
    }
}
